﻿using System.Collections.Generic;

namespace ProblemsSet
{
    public class Problem_33 : BaseProblem
    {
        public override object GetResult()
        {
            var nom = 1;
            var denom = 1;
            GetDen(out nom, out denom);
            var nod = MathLogic.GetNOD(nom, denom);
            return denom/nod;
        }

        private static List<string> GetDen(out int nom, out int denom)
        {
            var res = new List<string>();
            nom = 1;
            denom = 1;
            for (var b1 = 1; b1 < 10; b1++ )
            {
                for (var b2 = 1; b2 < 10; b2++)
                {
                    var a1 = 0;
                    var a2 = 0;
                    if (b1 >= b2)
                    {
                        a1 = b2;
                        for (a2 = 1; a2 < 10; a2++)
                        {
                            if (10*a1 + a2 >= 10*b1+b2) continue;
                            if ((10*a1+a2)*b1 != a2*(10*b1+b2)) continue;
                            res.Add(string.Format("{0}{1}/{2}{3}={1}/{2}", a1,a2, b1, b2));
                            nom *= a2;
                            denom *= b1;
                        }
                    }
                    
                    a1 = b1;
                    for (a2 = 1; a2 < b2; a2++)
                    {
                        if ((10 * a1 + a2) * b2 != a2 * (10 * b1 + b2)) continue;
                        res.Add(string.Format("{0}{1}/{2}{3}={1}/{3}", a1, a2, b1, b2));
                        nom *= a2;
                        denom *= b2;
                    }

                    
                    a2 = b1;
                    for (a1 = 1; a1 <= b1; a1++)
                    {
                        if (10 * a1 + a2 >= 10 * b1 + b2) continue;
                        if ((10 * a1 + a2) * b2 != a1 * (10 * b1 + b2)) continue;
                        res.Add(string.Format("{0}{1}/{2}{3}={0}/{3}", a1, a2, b1, b2));
                        nom *= a1;
                        denom *= b2;
                    }

                    a2 = b2;
                    for (a1 = 1; a1 < b1; a1++)
                    {
                        if ((10 * a1 + a2) * b1 != a1 * (10 * b1 + b2)) continue;
                        res.Add(string.Format("{0}{1}/{2}{3}={0}/{2}", a1, a2, b1, b2));
                        nom *= a1;
                        denom *= b1;
                    }

                }
            }
            return res;
        }

        public override string Problem
        {
            get
            {
                return @"The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.

We shall consider fractions like, 30/50 = 3/5, to be trivial examples.

There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 100;
            }
        }

    }
}
